<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button v-if="hasPermission(['system:role:add'])" type="primary" @click="handleCreate">
          新增账号
        </a-button>
        <a-button v-if="hasPermission(['system:role:export'])" type="primary" @click="handleExport">
          导出账号
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
                auth:'system:role:edit'
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此账号',
                auth:'system:role:remove',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
               {
                icon: 'ant-design:key-outlined',
                color: 'error',
                tooltip: '重置密码',
                // auth:'system:user:remove',
                 onClick: handleResetPews.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess"/>

    <ResetPwdModal @register="registerModalResetPwd" @success="handleSuccess"/>
  </PageWrapper>
</template>
<script lang="ts" setup>
import {reactive} from 'vue';
import {BasicTable, useTable, TableAction} from '@/components/Table';
import {PageWrapper} from '@/components/Page';
import {useModal} from '@/components/Modal';
import AccountModal from './AccountModal.vue';
import {columns, searchFormSchema} from './user.data';
import {useGo} from '@/hooks/web/usePage';
import {getRoleList} from "@/api/system/role";
import {deleteUser} from "@/api/system/user";
import {usePermission} from '@/hooks/web/usePermission';
import ResetPwdModal from "@/views/system/user/ResetPwdModal.vue";

const {hasPermission} = usePermission();

defineOptions({name: 'AccountManagement'});

const go = useGo();
const [registerModal, {openModal}] = useModal();
const [registerModalResetPwd, {openModal: openResetPwdModal}] = useModal();

const searchInfo = reactive<Recordable>({});
const [registerTable, {reload, getSearchInfo}] = useTable({
  title: '账号列表',
  api: getRoleList,
  rowKey: 'id',
  columns,
  fetchSetting: {
    // The field name of the current page passed to the background
    pageField: 'pageNum',
    // The number field name of each page displayed in the background
    sizeField: 'pageSize',
    // Field name of the form data returned by the interface
    listField: 'rows',
    // Total number of tables returned by the interface field name
    totalField: 'total',
  },
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  handleSearchInfoFn(info) {
    console.log('handleSearchInfoFn', info);
    return info;
  },
  actionColumn: {
    width: 120,
    title: '操作',
    dataIndex: 'action',
    // slots: { customRender: 'action' },
  },
});

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  console.log(record);
  openModal(true, {
    record,
    isUpdate: true,
  });
}

function handleDelete(record: Recordable) {
  deleteUser(record.userId);
  handleSuccess();
}

function handleExport() {
  console.log(getSearchInfo());
}

function handleSuccess() {
  reload();
}


//重置密码
function handleResetPews(record: Recordable){
  openResetPwdModal(true, {
    record,
    isUpdate: true,
  });
}
</script>
